const mysql = require("mysql");
const dbconfig = {
    host: '10.0.224.8',
    user: 'root',
    password: 't&Mr8P(F',
    database: 'nodejs_demo',
    port: 3306
}

class DBModel{
    constructor(){
        console.log("触发DBModel的构造方法");
        this.link = mysql.createConnection(dbconfig);
        this.link.connect();
    }
    execSQL(sqlStr,sqlParams=[]){
        // async/await
        let prom = new Promise( resolve=>{
            this.link.query(sqlStr,sqlParams,
                function(err,result){
                if(err){
                    resolve( {errno:1001,msg:"执行失败",err}  )
                    // throw err; //错误处理
                }
                resolve( {errno:0,msg:"执行成功",datas:result}  )
                // res= result
                // console.log("执行结果：",result);
            })
        } );
        this.link.end();//关闭数据库连接
        console.log("返回承诺对象：",prom);
        return prom;
    }
    // test(){
    //     console.log(this.link);
    //     return "测试模块调用结果！"
    // }
}

module.exports =DBModel